package com.sofun.ai;

import com.sofun.ai.assistant.AiAssistant;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.mcp.client.DefaultMcpClient;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.mcp.client.transport.McpTransport;
import dev.langchain4j.mcp.client.transport.stdio.StdioMcpTransport;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolProvider;

import java.util.Arrays;
import java.util.Collections;

public class McpExample {

    public static void main(String[] args) throws Exception {

/*        ChatLanguageModel model = OllamaChatModel.builder()
                .baseUrl("http://localhost:11434") // Ollama 默认本地服务地址
                .modelName("llama3-groq-tool-use:8b") // 你本地 Ollama 拉取的模型名称
                .logRequests(true)
                .logResponses(true)
                .build();*/

        ChatLanguageModel model = qwen();

        McpTransport transport = new StdioMcpTransport.Builder()
                .command(Arrays.asList("/usr/local/bin/docker", "run", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", "-i", "mcp/github"))
                .logEvents(true)
                .build();

        McpClient mcpClient = new DefaultMcpClient.Builder()
                .transport(transport)
                .build();

        ToolProvider toolProvider = McpToolProvider.builder()
                .mcpClients(Collections.singletonList(mcpClient))
                .build();

        AiAssistant bot = AiServices.builder(AiAssistant.class)
                .chatLanguageModel(model)
                .toolProvider(toolProvider)
                .build();

        try {
            String response = bot.chat(11,"Summarize the last 3 commits of the LangChain4j GitHub repository");
            System.out.println("RESPONSE: " + response);
        } finally {
            mcpClient.close();
        }
    }


    private static ChatLanguageModel qwen(){
        return QwenChatModel
                .builder()
                .baseUrl("https://dashscope.aliyuncs.com")
                .apiKey("sk-c473fa44f3b24ab195e0e3373083c773")
                .modelName("qwen-max")
                .build();
    }
}
